package com.shop.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.entity.Reduction;
import com.shop.mapper.ReductionMapper;
import com.shop.model.PageParam;
import com.shop.model.vo.Rsp;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 满减表
 * <p>
 * 陈炼豪(Reduction)表服务接口
 *
 * @author makejava
 * @since 2020-11-25 14:42:08
 */
@Service
@AllArgsConstructor
public class ReductionService {
    private final ReductionMapper reductionMapper;

    public Rsp getReductionByMerchantId(Integer id) {
        List<Reduction> reductions = reductionMapper.selectList(new QueryWrapper<Reduction>()
                //拿到商家审核后的
                .lambda().eq(Reduction::getStatus, 1)
                .eq(Reduction::getMerchantId, id)
                .eq(Reduction::getIsDeleted, "0"));
        return Rsp.success(reductions);
    }

    /**
     * @param pageParam
     * @param merchantId
     * @return com.shop.model.vo.Rsp
     * @author Clh
     * @description 获取店铺满减信息
     * @date 2020/11/26 11:50
     **/
    public PageInfo getReduction(PageParam<Reduction> pageParam, Integer merchantId) {
        pageParam.getModel().setMerchantId(merchantId);
        //开启分页
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        //排序
        for (int i = 0; i < pageParam.getOrderParams().length; i++) {
            PageHelper.orderBy(pageParam.getOrderParams()[i]);
        }
        List<Reduction> reductionList = reductionMapper.getReduction(pageParam.getModel());
        PageInfo<Reduction> pageInfo = new PageInfo(reductionList);
        return pageInfo;
    }
    /**
     * @author Clh
     * @description 添加店铺满减信息
     * @date  2020/11/26 14:40
     * @param reduction
     * @param merchantId
     * @param userId
     * @return com.shop.model.vo.Rsp
    **/
    public Rsp insertSelective(Reduction reduction,Integer merchantId,Integer userId){
        Integer count = reductionMapper.countByMerchantId(merchantId);
        if (count>=3){
            return Rsp.fail().msg("满减信息最多添加三条");
        }else {

            reduction.setMerchantId(merchantId);
            reduction.setCreateUserId(userId);
            return reductionMapper.insertSelective(reduction)>0?Rsp.success(null).msg("添加成功"):Rsp.fail().msg("添加失败");
        }
    }
    /**
     * @author Clh
     * @description 删除满减信息
     * @date  2020/11/26 15:06
     * @param
     * @return com.shop.model.vo.Rsp
    **/
    public Rsp deleteById(Reduction reduction){

        return  Rsp.success(reductionMapper.deleteById(reduction.getId()));
    }
    /**
     * @author Clh
     * @description 修改满减信息
     * @date  2020/11/26 15:57
     * @param reduction
     * @return com.shop.model.vo.Rsp
    **/
    public Rsp updateById(Reduction reduction,Integer userId){
            reduction.setUpdateUserId(userId);
        return Rsp.success(reductionMapper.updateById(reduction));
    }

    public List<Reduction> getReductionByMerchantId2(Integer id) {
        return reductionMapper.selectList(new QueryWrapper<Reduction>()
                //拿到商家审核后的
                .lambda().eq(Reduction::getStatus, 1)
                .eq(Reduction::getMerchantId, id)
                .eq(Reduction::getIsDeleted, "0"));
    }
}
